﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AteneoLibriBusinessLogic.Entities;

namespace AteneoLibri.BackEnd
{
    public partial class InsertFaculty : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                pnlInsertFaculty.Visible = false;
                pnlFaculties.Visible = false;
                pnlSelectUniversity.Visible = true;

                using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
                {
                    ddlUniversities.DataSource = service.getUniversities();
                    ddlUniversities.DataBind();
                }                 
            }
            else
            {
                pnlInsertFaculty.Visible = true;
                pnlFaculties.Visible = true;
                pnlSelectUniversity.Visible = false;

                using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
                {
                    string universityId = ddlUniversities.SelectedItem.Value;
                    if (universityId != "0")
                    {
                        Guid idUni = new Guid(universityId);
                        List<AteneoLibriBusinessLogic.Entities.Faculty> faculties = service.getFacultiesByUniversity(idUni);
                        uniName.Text = service.getUniversity(idUni).Name;

                        gvFaculties.DataSource = faculties;
                        gvFaculties.DataBind();
                    }
                    
                }
            }
        }

        protected void btnInsertFaculty_Click(object sender, EventArgs e)
        {
            string facultyName = txtFaculty.Text.Trim();
            if (facultyName != null && facultyName != "")
            {
                try
                {
                    Faculty faculty = new Faculty();
                    using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
                    {
                        faculty.IdFaculty = Guid.NewGuid();
                        faculty.Name = facultyName;
                        faculty.UniversityId = new Guid(ddlUniversities.SelectedItem.Value);

                        bool result = service.insertFaculty(faculty);
                        if (result)
                        {
                            // open panel

                            // refresh page
                            Response.Redirect("InsertFaculty.aspx");
                        }
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }

        protected void btnDeleteFaculty_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton button = sender as ImageButton;
            string idFaculty = button.CommandArgument;

            using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
            {
                Guid facultyId = new Guid(idFaculty);
                Faculty faculty = service.getFaculty(facultyId);

                List<User> students = service.getUsers().Where(x => x.FacultyId == facultyId).ToList();
                
                if (students.Count() == 0)
                {
                    var result = service.deleteFaculty(faculty);
                    if (result)
                    {
                        // refresh page
                        Response.Redirect("InsertFaculty.aspx");
                    }
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "Attenzione!", "alert('Cancellare prima tutte le facoltà dell'università prima di procedere alla rimozione dell'università.');", true);
                }



            }

        }


    }
}